/**
 * created by 小卷毛, 2020-05-20 020
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.aifeng.viewmodel

import com.aifeng.common_ui.helper.optNotNull
import com.aifeng.model.BaseUrl
import com.aifeng.model.CommonModel
import com.aifeng.utils.applyFlowableSchedulers
import com.aifeng.utils.applySchedulers
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.RxSingleHelper
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import io.reactivex.Flowable
import org.json.JSONObject

/**
 * 项目名称：Hassan_Food
 * 创建人：小卷毛
 * 创建时间：2020-05-20 17:21
 */
class WalletViewModel : BaseViewModel() {

    fun balanceList(page: Int, userId: String) =
        OkGo.post<BaseResponse<CommonModel>>(BaseUrl.balanceList)
            .params(
                getParams(
                    "page" to page,
                    "userid" to userId
                )
            )
            .converter(object : JacksonConvert<BaseResponse<CommonModel>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.results }

    fun bankName(card: String, data: String): Flowable<String> =
        OkGo.get<String>("https://ccdcapi.alipay.com/validateAndCacheCardInfo.json")
            .params("_input_charset", "utf-8")
            .params("cardNo", card)
            .params("cardBinCheck", true)
            .converter(object : JsonConvert<String>() {})
            .adapt(FlowableBody())
            .map {
                val jsonName = JSONObject(data)
                val json = JSONObject(it)
                if (json.has("bank")) {
                    return@map jsonName.optNotNull(json.optString("bank"))
                } else {
                    return@map ""
                }
            }
            .compose(applyFlowableSchedulers())

    fun bank(vararg params: Pair<String, Any>) =
        RxSingleHelper.requestString(
            url = BaseUrl.editInfo,
            params = getParams(*params)
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    fun unbind(userId: String, pwd: String) =
        RxSingleHelper.requestString(
            url = BaseUrl.unbind,
            params = getParams(
                "userid" to userId,
                "paypwd" to pwd
            )
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    fun withdraw(vararg params: Pair<String, Any>) =
        RxSingleHelper.requestString(
            url = BaseUrl.withdraw,
            params = getParams(*params)
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

}